#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "web_server.h"
#include "http_server_impl.h"
#include <thread>
#include <chrono>

using namespace El::WebServer;
using namespace std::chrono_literals;

class ReverseProxyTest : public ::testing::Test {
protected:
    void SetUp() override {
        server_ = &HttpServerImpl::GetInstance();
    }

    void TearDown() override {
        server_->Stop();
        // 清理所有代理规则
        server_->RemoveProxyRule("/api/.*");
        server_->RemoveProxyRule("/service/.*");
        server_->RemoveProxyRule("/backend/.*");
    }

    HttpServerImpl* server_;
};

// 测试代理规则注册
TEST_F(ReverseProxyTest, RegisterProxyRule) {
    std::vector<ProxyTarget> targets = {
        ProxyTarget("127.0.0.1", 8080, false, 1, true),
        ProxyTarget("127.0.0.1", 8081, false, 1, true)
    };
    
    ProxyOptions options;
    options.timeout_ms = 3000;
    options.retry_count = 2;
    
    // 注册代理规则应该成功
    EXPECT_TRUE(server_->RegisterProxyRule("/api/.*", targets, options));
    
    // 重复注册相同规则应该失败
    EXPECT_FALSE(server_->RegisterProxyRule("/api/.*", targets, options));
}

// 测试代理规则移除
TEST_F(ReverseProxyTest, RemoveProxyRule) {
    std::vector<ProxyTarget> targets = {
        ProxyTarget("127.0.0.1", 8080, false, 1, true)
    };
    
    // 先注册规则
    EXPECT_TRUE(server_->RegisterProxyRule("/api/.*", targets));
    
    // 移除规则应该成功
    EXPECT_TRUE(server_->RemoveProxyRule("/api/.*"));
    
    // 移除不存在的规则应该失败
    EXPECT_FALSE(server_->RemoveProxyRule("/nonexistent/.*"));
}

// 测试代理目标配置
TEST_F(ReverseProxyTest, ProxyTargetConfiguration) {
    // 测试默认构造函数
    ProxyTarget default_target;
    EXPECT_EQ(default_target.port, 80);
    EXPECT_FALSE(default_target.use_https);
    EXPECT_EQ(default_target.weight, 1);
    EXPECT_TRUE(default_target.enabled);
    
    // 测试带参数构造函数
    ProxyTarget custom_target("192.168.1.100", 8443, true, 5, false);
    EXPECT_EQ(custom_target.host, "192.168.1.100");
    EXPECT_EQ(custom_target.port, 8443);
    EXPECT_TRUE(custom_target.use_https);
    EXPECT_EQ(custom_target.weight, 5);
    EXPECT_FALSE(custom_target.enabled);
}

// 测试代理选项配置
TEST_F(ReverseProxyTest, ProxyOptionsConfiguration) {
    ProxyOptions options;
    
    // 测试默认值
    EXPECT_EQ(options.timeout_ms, 5000);
    EXPECT_EQ(options.retry_count, 3);
    EXPECT_FALSE(options.preserve_host);
    EXPECT_EQ(options.strip_prefix, "");
    EXPECT_EQ(options.add_prefix, "");
    EXPECT_TRUE(options.headers.empty());
    
    // 测试自定义配置
    options.timeout_ms = 10000;
    options.retry_count = 5;
    options.preserve_host = true;
    options.strip_prefix = "/api";
    options.add_prefix = "/v1";
    options.headers["X-Custom-Header"] = "test-value";
    
    EXPECT_EQ(options.timeout_ms, 10000);
    EXPECT_EQ(options.retry_count, 5);
    EXPECT_TRUE(options.preserve_host);
    EXPECT_EQ(options.strip_prefix, "/api");
    EXPECT_EQ(options.add_prefix, "/v1");
    EXPECT_EQ(options.headers["X-Custom-Header"], "test-value");
}

// 测试全局代理配置
TEST_F(ReverseProxyTest, GlobalProxyConfiguration) {
    // 测试设置全局超时
    server_->SetProxyGlobalTimeout(8000);
    
    // 测试设置全局重试次数
    server_->SetProxyGlobalRetry(5);
    
    // 测试启用代理日志
    server_->EnableProxyLogging(true);
    server_->EnableProxyLogging(false);
}

// 测试多个代理规则
TEST_F(ReverseProxyTest, MultipleProxyRules) {
    std::vector<ProxyTarget> api_targets = {
        ProxyTarget("127.0.0.1", 8080)
    };
    
    std::vector<ProxyTarget> service_targets = {
        ProxyTarget("127.0.0.1", 9000),
        ProxyTarget("127.0.0.1", 9001)
    };
    
    // 注册多个代理规则
    EXPECT_TRUE(server_->RegisterProxyRule("/api/.*", api_targets));
    EXPECT_TRUE(server_->RegisterProxyRule("/service/.*", service_targets));
    
    // 验证都能正确注册
    EXPECT_FALSE(server_->RegisterProxyRule("/api/.*", api_targets)); // 重复注册失败
    EXPECT_FALSE(server_->RegisterProxyRule("/service/.*", service_targets)); // 重复注册失败
}

// 测试负载均衡配置
TEST_F(ReverseProxyTest, LoadBalancingConfiguration) {
    std::vector<ProxyTarget> targets = {
        ProxyTarget("127.0.0.1", 8080, false, 3, true),
        ProxyTarget("127.0.0.1", 8081, false, 2, true),
        ProxyTarget("127.0.0.1", 8082, false, 1, false) // 禁用的目标
    };
    
    ProxyOptions options;
    options.timeout_ms = 3000;
    
    EXPECT_TRUE(server_->RegisterProxyRule("/backend/.*", targets, options));
}

// 测试HTTPS代理目标
TEST_F(ReverseProxyTest, HTTPSProxyTarget) {
    std::vector<ProxyTarget> targets = {
        ProxyTarget("api.example.com", 443, true, 1, true)
    };
    
    ProxyOptions options;
    options.preserve_host = true;
    options.headers["Authorization"] = "Bearer token123";
    
    EXPECT_TRUE(server_->RegisterProxyRule("/external/.*", targets, options));
}

// 测试URL路径重写
TEST_F(ReverseProxyTest, URLPathRewriting) {
    std::vector<ProxyTarget> targets = {
        ProxyTarget("127.0.0.1", 8080)
    };
    
    ProxyOptions options;
    options.strip_prefix = "/api/v1";
    options.add_prefix = "/internal";
    
    EXPECT_TRUE(server_->RegisterProxyRule("/api/v1/.*", targets, options));
}

// 测试代理规则优先级（正则表达式匹配）
TEST_F(ReverseProxyTest, ProxyRulePriority) {
    std::vector<ProxyTarget> specific_targets = {
        ProxyTarget("127.0.0.1", 8080)
    };
    
    std::vector<ProxyTarget> general_targets = {
        ProxyTarget("127.0.0.1", 9000)
    };
    
    // 注册具体的规则
    EXPECT_TRUE(server_->RegisterProxyRule("/api/users/.*", specific_targets));
    
    // 注册一般的规则
    EXPECT_TRUE(server_->RegisterProxyRule("/api/.*", general_targets));
}